package com.csh.szhm.activity;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.csh.szhm.R;
import com.csh.szhm.adapter.SzhmJfdhMainAdapter;
import com.csh.szhm.adapter.SzhmSelectAreaLeftAdapter;
import com.csh.szhm.adapter.SzhmSelectAreaRightAdapter;
import com.csh.szhm.utils.AppConfig;
import com.csh.szhm.utils.StringUtil;

/**
 * 两个选择条件
 * 
 * @author audrey
 *
 */
public class SzhmSortTwoMainActiviy extends MyRefreshBaseActivity<String> {

	private int[] imgArr = new int[] { R.id.sort_1_img, R.id.sort_2_img };

	@Override
	protected void init() {
		initContentView(R.layout.szhm_jf_main_layout);
		setMyTitle("积分兑换");
		super.init();
		mAdapter = new SzhmJfdhMainAdapter(this);
		mLikeListView.setAdapter(mAdapter);
		mLikeListView.setSelector(getResources().getDrawable(
				R.drawable.all_select));
		mLikeListView.setBackgroundColor(getResources().getColor(
				R.color.main_bg));

	}

	private ListView mLeftListView;
	private ListView mRightListView;
	private PopupWindow mPopupWindow;
	private SzhmSelectAreaLeftAdapter mSzhmSelectAreaLeftAdapter;
	private SzhmSelectAreaRightAdapter mSzhmSelectAreaRightAdapter;

	/**
	 * 初始化搜索条件弹出框
	 * 
	 * @param id
	 */
	public void initpopupwindow(int id) {
		if (mLeftListView == null) {
			LayoutInflater layoutInflater = LayoutInflater.from(this);
			View popupwindow = layoutInflater.inflate(
					R.layout.szhm_sort_select_layout, null);
			popupwindow.findViewById(R.id.popu_buttom_root_id)
					.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							mPopupWindow.dismiss();
							setImgPisition(-1);
						}
					});
			mLeftListView = (ListView) popupwindow
					.findViewById(R.id.lv_cla_area);
			mRightListView = (ListView) popupwindow
					.findViewById(R.id.lv_cla_area_next);
			mLeftListView.setCacheColorHint(Color.TRANSPARENT);// 删滑动背景
			// lv_cla_area.setBackgroundResource(R.drawable.clas_pop_left_back_1);
			mRightListView.setCacheColorHint(Color.TRANSPARENT);
			mLeftListView.setDivider(null);
			mRightListView.setDivider(null);
			mLeftListView.setSelector(R.drawable.all_select);
			mRightListView.setSelector(R.drawable.all_select);
			mSzhmSelectAreaLeftAdapter = new SzhmSelectAreaLeftAdapter(this);
			mSzhmSelectAreaRightAdapter = new SzhmSelectAreaRightAdapter(this);
			mLeftListView.setAdapter(mSzhmSelectAreaLeftAdapter);
			mRightListView.setAdapter(mSzhmSelectAreaRightAdapter);
			mPopupWindow = new PopupWindow(popupwindow,
					AppConfig.getScreenWidth(), AppConfig.getScreenHeight());
			mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
			mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
			mPopupWindow.setOutsideTouchable(true);
			mPopupWindow.setFocusable(true);
			mPopupWindow.setOnDismissListener(new OnDismissListener() {

				@Override
				public void onDismiss() {
					// TODO Auto-generated method stub
					setImgPisition(-1);
				}
			});
			// mPopupWindow.setOnDismissListener(new
			// MyOnDismissListener());//点击pop之外的地方消息
		}
		if (id == R.id.sort_2) {
			// 只有一种情况
			mLeftListView.setVisibility(View.GONE);
		} else {
			mLeftListView.setVisibility(View.VISIBLE);
		}
		mLeftListView.setOnItemClickListener(new MyItemClickListener(id));
		mRightListView.setOnItemClickListener(new MySubItemClickListener(id));
		mSzhmSelectAreaLeftAdapter.setSelectPosition(getLeftPosition(id)[0]);
		mSzhmSelectAreaRightAdapter.setSelectPosition(getLeftPosition(id)[1]);
		mLeftListView.setSelection(getLeftPosition(id)[0]);
		mRightListView.setSelection(getLeftPosition(id)[1]);
	}

	private int[] getLeftPosition(int id) {
		int[] num = new int[] { 0, 0 };
		String str = (String) findViewById(id).getTag();
		if (StringUtil.isEmptyString(str)) {
			return num;
		}
		String[] st = str.split("-");
		num[0] = Integer.valueOf(st[0]);
		num[1] = Integer.valueOf(st[1]);
		return num;
	}

	private void setImgPisition(int id) {
		for (int i = 0; i < imgArr.length; i++) {
			if (id == imgArr[i]) {
				findViewById(imgArr[i]).setVisibility(View.VISIBLE);
			} else {
				findViewById(imgArr[i]).setVisibility(View.GONE);
			}
		}
	}

	private int leftSelectPostion;
	private int rightSelectPostion;

	private class MyItemClickListener implements OnItemClickListener {

		private int viewId;

		public MyItemClickListener(int id) {
			viewId = id;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			findViewById(viewId).setTag(arg2 + "-" + rightSelectPostion);
			leftSelectPostion = arg2;
			mSzhmSelectAreaLeftAdapter.setSelectPosition(arg2);
//			mSzhmSelectAreaRightAdapter.count = 20;
			mSzhmSelectAreaRightAdapter.notifyDataSetChanged();
		}

	}

	private class MySubItemClickListener implements OnItemClickListener {
		private int viewId;

		public MySubItemClickListener(int id) {
			viewId = id;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			mPopupWindow.dismiss();
			findViewById(viewId).setTag(leftSelectPostion + "-" + arg2);
			setImgPisition(-1);
		}

	}

	@Override
	public void click(View v) {
		switch (v.getId()) {
		case R.id.sort_1:
			initpopupwindow(v.getId());
			mPopupWindow.showAsDropDown(v);
			setImgPisition(imgArr[0]);
			break;
		case R.id.sort_2:
			initpopupwindow(v.getId());
			mPopupWindow.showAsDropDown(v);
			setImgPisition(imgArr[1]);
			break;
		}
		super.click(v);
	}

	@Override
	protected void onMyItemClick(String item) {
		startMyActivity(SzhmShopDetailActivity.class);
		super.onMyItemClick(item);
	}

}
